﻿#include "UI/newUI/SetScriptDialog.h"

#include "qlist.h"
#include "qmessagebox.h"
#include "ACT/Script/ScriptRunner.h"
#include "ACT/Script/ScriptInstructionFactory.h"

SetScriptDialog::SetScriptDialog(QWidget* parent) :QDialog(parent)
{
	ui.setupUi(this);
	initSignalsAndSlots();
	ui.pushButton_script_edit->setDisabled(true);
}
SetScriptDialog::~SetScriptDialog() {}

int SetScriptDialog::exec()
{
	ui.treeWidget_script_list->showScriptList();
	return QDialog::exec();
}

void SetScriptDialog::accept()
{
	if (ui.treeWidget_script_list->updateScriptList())
	{
		QDialog::accept();
	}
	return;
}

void SetScriptDialog::slot_openSetPositionModeDialog()
{
	if (QDialog::Accepted == setPositionModeDialog.exec())
	{
		QString setPositionModeString = ScriptInstructionFactory::getDescribe_TurnTable_PositionMode(
			setPositionModeDialog.getPosition(), 
			setPositionModeDialog.getRate(), 
			setPositionModeDialog.getAcceleration());
		ui.treeWidget_script_list->addOperationNameToList(setPositionModeString);
	}
}
void SetScriptDialog::slot_openSetRateModeDialog()
{
	if (QDialog::Accepted == setRateModeDialog.exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_RateMode(setRateModeDialog.getRate(), setRateModeDialog.getAcceleration()));
	}
}
void SetScriptDialog::slot_openSetThermostatDialog()
{
	if (QDialog::Accepted == setThermostatDialog.exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_Thermostat_TemperatureRate(setThermostatDialog.getRate()));
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_Thermostat_TargetTemperature(setThermostatDialog.getTemperature()));
	}
}
void SetScriptDialog::slot_openSetWaitTimeDialog()
{
	if (QDialog::Accepted == setScriptWaitDialog.exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_Wait(setScriptWaitDialog.getWaitTime()));
	}
}
void SetScriptDialog::slot_openSetLoopDialog()
{
	if (QDialog::Accepted == setScriptLoopDialog.exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_Loop(setScriptLoopDialog.getLoopCount()));
	}
}
void SetScriptDialog::slot_edit() {}


void SetScriptDialog::initSignalsAndSlots()
{
	connect(ui.pushButton_turnTable_powerOn, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_PowerOn()); });
	connect(ui.pushButton_turnTable_powerOff, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_PowerOff()); });
	connect(ui.pushButton_turnTable_closeCircuit, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_CloseCircuit()); });
	connect(ui.pushButton_turnTable_openCircuit, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_OpenCircuit()); });
	connect(ui.pushButton_turnTable_unloadedMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_SetLoad(TurnTableCodec::Mode_Unloaded)); });
	connect(ui.pushButton_turnTable_loadedMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_SetLoad(TurnTableCodec::Mode_Loaded)); });
	connect(ui.pushButton_turnTable_setPositionMode, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetPositionModeDialog);
	connect(ui.pushButton_turnTable_setRateMode, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetRateModeDialog);
	connect(ui.pushButton_turnTable_runPositionMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_Run(TurnTableCodec::Mode_PositionMode)); });
	connect(ui.pushButton_turnTable_runRateMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_Run(TurnTableCodec::Mode_RateMode)); });
	connect(ui.pushButton_thermostat_setTemperatureAndRate, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetThermostatDialog);
	connect(ui.pushButton_turnTable_stop, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_Stop()); });
	connect(ui.pushButton_turnTable_emergencyStop, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_EmergencyStop()); });
	connect(ui.pushButton_dataReader_startCollection, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_DataReader_StartCollection()); });
	connect(ui.pushButton_dataReader_stopCollection, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_DataReader_StopCollection()); });
	connect(ui.pushButton_script_confirm, &QPushButton::clicked, this, &SetScriptDialog::accept);
	connect(ui.pushButton_script_cancel, &QPushButton::clicked, this, &SetScriptDialog::reject);
	connect(ui.pushButton_script_wait, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetWaitTimeDialog);
	connect(ui.pushButton_script_creatLoop, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetLoopDialog);
	connect(ui.pushButton_script_edit, &QPushButton::clicked, this, &SetScriptDialog::slot_edit);
	connect(ui.pushButton_script_clone, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::Clone); });
	connect(ui.pushButton_script_moveUp, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::MoveUp); });
	connect(ui.pushButton_script_moveDown, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::MoveDown); });
	connect(ui.pushButton_script_delete, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::Remove); });
}

int SetScriptDialog::getAccuracy()
{
	return setPositionModeDialog.getAccuracy();
}

void SetScriptDialog::setAccuracy(int accuracy)
{
	setPositionModeDialog.setAccuracy(accuracy);
	setRateModeDialog.setAccuracy(accuracy);
	setThermostatDialog.setAccuracy(accuracy);
}

float SetScriptDialog::getTurnTablePositionUpperLimit()
{
	return setPositionModeDialog.getTurnTablePositionUpperLimit();
}

void SetScriptDialog::setTurnTablePositionUpperLimit(float turnTablePositionUpperLimit)
{
	setPositionModeDialog.setTurnTablePositionUpperLimit(turnTablePositionUpperLimit);
}

float SetScriptDialog::getTurnTablePositionLowerLimit()
{
	return setPositionModeDialog.getTurnTablePositionLowerLimit();
}

void SetScriptDialog::setTurnTablePositionLowerLimit(float turnTablePositionLowerLimit)
{
	setPositionModeDialog.setTurnTablePositionLowerLimit(turnTablePositionLowerLimit);
}

float SetScriptDialog::getTurnTableRateUpperLimit()
{
	return setPositionModeDialog.getTurnTableRateUpperLimit();
}

void SetScriptDialog::setTurnTableRateUpperLimit(float turnTableRateUpperLimit)
{
	setPositionModeDialog.setTurnTableRateUpperLimit(turnTableRateUpperLimit);
	setRateModeDialog.setTurnTableRateUpperLimit(turnTableRateUpperLimit);
}

float SetScriptDialog::getTurnTableRateLowerLimit()
{
	return setPositionModeDialog.getTurnTableRateLowerLimit();
}

void SetScriptDialog::setTurnTableRateLowerLimit(float turnTableRateLowerLimit)
{
	setPositionModeDialog.setTurnTableRateLowerLimit(turnTableRateLowerLimit);
	setRateModeDialog.setTurnTableRateLowerLimit(turnTableRateLowerLimit);
}

float SetScriptDialog::getTurnTableAccelerationUpperLimit()
{
	return setPositionModeDialog.getTurnTableAccelerationUpperLimit();
}

void SetScriptDialog::setTurnTableAccelerationUpperLimit(float  turnTableAccelerationUpperLimit)
{
	setPositionModeDialog.setTurnTableAccelerationUpperLimit(turnTableAccelerationUpperLimit);
	setRateModeDialog.setTurnTableAccelerationUpperLimit(turnTableAccelerationUpperLimit);
}

float SetScriptDialog::getTurnTableAccelerationLowerLimit()
{
	return setPositionModeDialog.getTurnTableAccelerationLowerLimit();
}

void SetScriptDialog::setTurnTableAccelerationLowerLimit(float turnTableAccelerationLowerLimit)
{
	setPositionModeDialog.setTurnTableAccelerationLowerLimit(turnTableAccelerationLowerLimit);
	setRateModeDialog.setTurnTableAccelerationLowerLimit(turnTableAccelerationLowerLimit);
}

ScriptRunner* SetScriptDialog::getScriptRunner()
{
	return scriptRunner;
}
void SetScriptDialog::setScriptRunner(ScriptRunner* scriptRunner)
{
	this->scriptRunner = scriptRunner;
	ui.treeWidget_script_list->setScriptRunner(this->scriptRunner);
	printInjection(this, scriptRunner);
}

void SetScriptDialog::objectAutoInjection(QHash<QString, QObject*>* objMap)
{
	setScriptRunner(getObjFromMap<ScriptRunner>(objMap));
}